﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;
using System.Data;

namespace DAO
{
    public class PhieuThuTienDAO
    {
        public static int ThemPhieuThu(PhieuThuTienDTO dv)
        {
            string sql;
            sql = "Insert into PhieuThuTien (MaPhieuThuTien,NgayThuTien,SoTienThu,MaKhachHang,TrangThai) values ('" + dv.MaPhieuThu  + "','" + dv.NgayThuTien + "', " + dv.SoTienThu  + " ,'" + dv.KhachHang.MaKhachHang + "' , " + dv.TrangThai + ")";
  
            int kq = DataAccess.ExcuNonQuery(sql);
            //int kq = DataAccess.ExcuNonQuery(sql);

            return kq;
        }

        public static PhieuThuTienDTO LayThongTinPhieuThu(String maPT)
        {
            PhieuThuTienDTO pt = new PhieuThuTienDTO();
            string sql = "Select * From PhieuThuTien Where MaPhieuThuTien = '" + maPT + "'";
            DataTable dt = DataAccess.ExcuQuery(sql);

            if (dt != null && dt.Rows.Count > 0)
            {
                pt.MaPhieuThu = Convert.ToString(dt.Rows[0]["MaPhieuThuTien"]);
                pt.NgayThuTien = Convert.ToDateTime(dt.Rows[0]["NgayThuTien"]);
                pt.TrangThai = Convert.ToInt32(dt.Rows[0]["TrangThai"]);
                pt.SoTienThu = float.Parse(dt.Rows[0]["SoTienThu"].ToString());
                pt.KhachHang = KhachHangDAO.LayThongTinKhachHang(dt.Rows[0]["MaKhachHang"].ToString());
            }
            return pt;
        }
        public static int Xoa(String ma)
        {
            string sql;
            sql = "Update PhieuThuTien  Set TrangThai=0 where MaPhieuThuTien = '" + ma + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static int CapNhat(PhieuThuTienDTO pt)
        {
            string sql;
            sql = "Update PhieuThuTien  Set MaKhachHang='" + pt.KhachHang.MaKhachHang + "',SoTienThu=" + pt.SoTienThu + ", NgayThuTien='" + pt.NgayThuTien + "', TrangThai= " + pt.TrangThai + " where MaPhieuThuTien = '" + pt.MaPhieuThu + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static ArrayList LayDanhSach()
        {
            ArrayList arr = new ArrayList();
            string sql;
            sql = "Select * From PhieuThuTien where TrangThai=1";
            DataTable dt = DataAccess.ExcuQuery(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PhieuThuTienDTO dv = new PhieuThuTienDTO();
                dv.MaPhieuThu = Convert.ToString(dt.Rows[i]["MaPhieuThuTien"]);
                dv.NgayThuTien = Convert.ToDateTime(dt.Rows[i]["NgayThuTien"]);
                dv.TrangThai = Convert.ToInt32(dt.Rows[i]["TrangThai"]);
                dv.SoTienThu = float.Parse(dt.Rows[i]["SoTienThu"].ToString());
                dv.KhachHang = KhachHangDAO.LayThongTinKhachHang(dt.Rows[i]["MaKhachHang"].ToString());
                arr.Add(dv);
            }
            return arr;
        }
    }
}
